﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TestPhone.Resources;
using Microsoft.Phone.Storage;

namespace TestPhone
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();

            // 用于本地化 ApplicationBar 的示例代码
            // BuildLocalizedApplicationBar();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }
        async void GetSDCard()
        {
            var sdlist = await ExternalStorage.GetExternalStorageDevicesAsync();
            if (sdlist.Count() > 0)
            {
                var sd = sdlist.First();
                var root = sd.RootFolder;
                var files = await root.GetFilesAsync();
                var f = files.FirstOrDefault();
                if (f != null)
                {
                    var stm = await f.OpenForReadAsync();
                    //System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    var iso = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
                    var ms = iso.OpenFile("temp.mkv", System.IO.FileMode.Create);
                    stm.CopyTo(ms);
                    stm.Close();
                    ms.Position = 0;
                    MyMediaStreamSource src = new MyMediaStreamSource(ms);
                    MyPlayer.SetSource(src);
                    MyPlayer.Play();
                }
            }
        }

        void GetSampleFile()
        {
            var iso = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
            if (!iso.FileExists("temp.mkv"))
            {
                GetSDCard();
            }
            else
            {
                var ms = iso.OpenFile("temp.mkv", System.IO.FileMode.Open);
                ms.Position = 0;
                MyMediaStreamSource src = new MyMediaStreamSource(ms);
                MyPlayer.SetSource(src);
                MyPlayer.Play();
            }
        }

        private void MyPlayer_MediaFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void MyPlayer_MediaOpened(object sender, RoutedEventArgs e)
        {

        }

        private void MyPlayer_MediaEnded(object sender, RoutedEventArgs e)
        {

        }
        // 用于生成本地化 ApplicationBar 的示例代码
        private void BuildLocalizedApplicationBar()
        {
            // 将页面的 ApplicationBar 设置为 ApplicationBar 的新实例。
            ApplicationBar = new ApplicationBar();

            // 创建新按钮并将文本值设置为 AppResources 中的本地化字符串。
            ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
            appBarButton.Click += appBarButton_Click;
            appBarButton.Text = "播放";//AppResources.AppBarButtonText;
            ApplicationBar.Buttons.Add(appBarButton);

            // 使用 AppResources 中的本地化字符串创建新菜单项。
            //ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
            //ApplicationBar.MenuItems.Add(appBarMenuItem);
        }

        void appBarButton_Click(object sender, EventArgs e)
        {
            GetSDCard();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            //GetSDCard();
            GetSampleFile();
        }
    }
}